home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tk8.0 / generic / tkImgBmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-15  |  30.8 KB  |  1,062 lines  |  [TEXT/CWIE]

  1. /* 
  2.  * tkImgBmap.c --
  3.  *
  4.  *    This procedure implements images of type "bitmap" for Tk.
  5.  *
  6.  * Copyright (c) 1994 The Regents of the University of California.
  7.  * Copyright (c) 1994-1997 Sun Microsystems, Inc.
  8.  *
  9.  * See the file "license.terms" for information on usage and redistribution
  10.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11.  *
  12.  * SCCS: @(#) tkImgBmap.c 1.33 97/07/31 09:08:22
  13.  */
  14.  
  15. #include "tkInt.h"
  16. #include "tkPort.h"
  17.  
  18. /*
  19.  * The following data structure represents the master for a bitmap
  20.  * image:
  21.  */
  22.  
  23. typedef struct BitmapMaster {
  24.     Tk_ImageMaster tkMaster;    /* Tk's token for image master.  NULL means
  25.                  * the image is being deleted. */
  26.     Tcl_Interp *interp;        /* Interpreter for application that is
  27.                  * using image. */
  28.     Tcl_Command imageCmd;    /* Token for image command (used to delete
  29.                  * it when the image goes away).  NULL means
  30.                  * the image command has already been
  31.                  * deleted. */
  32.     int width, height;        /* Dimensions of image. */
  33.     char *data;            /* Data comprising bitmap (suitable for
  34.                  * input to XCreateBitmapFromData).   May
  35.                  * be NULL if no data.  Malloc'ed. */
  36.     char *maskData;        /* Data for bitmap's mask (suitable for
  37.                  * input to XCreateBitmapFromData).
  38.                  * Malloc'ed. */
  39.     Tk_Uid fgUid;        /* Value of -foreground option (malloc'ed). */
  40.     Tk_Uid bgUid;        /* Value of -background option (malloc'ed). */
  41.     char *fileString;        /* Value of -file option (malloc'ed). */
  42.     char *dataString;        /* Value of -data option (malloc'ed). */
  43.     char *maskFileString;    /* Value of -maskfile option (malloc'ed). */
  44.     char *maskDataString;    /* Value of -maskdata option (malloc'ed). */
  45.     struct BitmapInstance *instancePtr;
  46.                 /* First in list of all instances associated
  47.                  * with this master. */
  48. } BitmapMaster;
  49.  
  50. /*
  51.  * The following data structure represents all of the instances of an
  52.  * image that lie within a particular window:
  53.  */
  54.  
  55. typedef struct BitmapInstance {
  56.     int refCount;        /* Number of instances that share this
  57.                  * data structure. */
  58.     BitmapMaster *masterPtr;    /* Pointer to master for image. */
  59.     Tk_Window tkwin;        /* Window in which the instances will be
  60.                  * displayed. */
  61.     XColor *fg;            /* Foreground color for displaying image. */
  62.     XColor *bg;            /* Background color for displaying image. */
  63.     Pixmap bitmap;        /* The bitmap to display. */
  64.     Pixmap mask;        /* Mask: only display bitmap pixels where
  65.                  * there are 1's here. */
  66.     GC gc;            /* Graphics context for displaying bitmap.
  67.                  * None means there was an error while
  68.                  * setting up the instance, so it cannot
  69.                  * be displayed. */
  70.     struct BitmapInstance *nextPtr;
  71.                 /* Next in list of all instance structures
  72.                  * associated with masterPtr (NULL means
  73.                  * end of list). */
  74. } BitmapInstance;
  75.  
  76. /*
  77.  * The type record for bitmap images:
  78.  */
  79.  
  80. static int        GetByte _ANSI_ARGS_((Tcl_Channel chan));
  81. static int        ImgBmapCreate _ANSI_ARGS_((Tcl_Interp *interp,
  82.                 char *name, int argc, char **argv,
  83.                 Tk_ImageType *typePtr, Tk_ImageMaster master,
  84.                 ClientData *clientDataPtr));
  85. static ClientData    ImgBmapGet _ANSI_ARGS_((Tk_Window tkwin,
  86.                 ClientData clientData));
  87. static void        ImgBmapDisplay _ANSI_ARGS_((ClientData clientData,
  88.                 Display *display, Drawable drawable, 
  89.                 int imageX, int imageY, int width, int height,
  90.                 int drawableX, int drawableY));
  91. static void        ImgBmapFree _ANSI_ARGS_((ClientData clientData,
  92.                 Display *display));
  93. static void        ImgBmapDelete _ANSI_ARGS_((ClientData clientData));
  94.  
  95. Tk_ImageType tkBitmapImageType = {
  96.     "bitmap",            /* name */
  97.     ImgBmapCreate,        /* createProc */
  98.     ImgBmapGet,            /* getProc */
  99.     ImgBmapDisplay,        /* displayProc */
  100.     ImgBmapFree,        /* freeProc */
  101.     ImgBmapDelete,        /* deleteProc */
  102.     (Tk_ImageType *) NULL    /* nextPtr */
  103. };
  104.  
  105. /*
  106.  * Information used for parsing configuration specs:
  107.  */
  108.  
  109. static Tk_ConfigSpec configSpecs[] = {
  110.     {TK_CONFIG_UID, "-background", (char *) NULL, (char *) NULL,
  111.     "", Tk_Offset(BitmapMaster, bgUid), 0},
  112.     {TK_CONFIG_STRING, "-data", (char *) NULL, (char *) NULL,
  113.     (char *) NULL, Tk_Offset(BitmapMaster, dataString), TK_CONFIG_NULL_OK},
  114.     {TK_CONFIG_STRING, "-file", (char *) NULL, (char *) NULL,
  115.     (char *) NULL, Tk_Offset(BitmapMaster, fileString), TK_CONFIG_NULL_OK},
  116.     {TK_CONFIG_UID, "-foreground", (char *) NULL, (char *) NULL,
  117.     "#000000", Tk_Offset(BitmapMaster, fgUid), 0},
  118.     {TK_CONFIG_STRING, "-maskdata", (char *) NULL, (char *) NULL,
  119.     (char *) NULL, Tk_Offset(BitmapMaster, maskDataString),
  120.     TK_CONFIG_NULL_OK},
  121.     {TK_CONFIG_STRING, "-maskfile", (char *) NULL, (char *) NULL,
  122.     (char *) NULL, Tk_Offset(BitmapMaster, maskFileString),
  123.     TK_CONFIG_NULL_OK},
  124.     {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
  125.     (char *) NULL, 0, 0}
  126. };
  127.  
  128. /*
  129.  * The following data structure is used to describe the state of
  130.  * parsing a bitmap file or string.  It is used for communication
  131.  * between TkGetBitmapData and NextBitmapWord.
  132.  */
  133.  
  134. #define MAX_WORD_LENGTH 100
  135. typedef struct ParseInfo {
  136.     char *string;        /* Next character of string data for bitmap,
  137.                  * or NULL if bitmap is being read from
  138.                  * file. */
  139.     Tcl_Channel chan;        /* File containing bitmap data, or NULL
  140.                  * if no file. */
  141.     char word[MAX_WORD_LENGTH+1];
  142.                 /* Current word of bitmap data, NULL
  143.                  * terminated. */
  144.     int wordLength;        /* Number of non-NULL bytes in word. */
  145. } ParseInfo;
  146.  
  147. /*
  148.  * Prototypes for procedures used only locally in this file:
  149.  */
  150.  
  151. static int        ImgBmapCmd _ANSI_ARGS_((ClientData clientData,
  152.                 Tcl_Interp *interp, int argc, char **argv));
  153. static void        ImgBmapCmdDeletedProc _ANSI_ARGS_((
  154.                 ClientData clientData));
  155. static void        ImgBmapConfigureInstance _ANSI_ARGS_((
  156.                 BitmapInstance *instancePtr));
  157. static int        ImgBmapConfigureMaster _ANSI_ARGS_((
  158.                 BitmapMaster *masterPtr, int argc, char **argv,
  159.                 int flags));
  160. static int        NextBitmapWord _ANSI_ARGS_((ParseInfo *parseInfoPtr));
  161.  
  162. /*
  163.  *----------------------------------------------------------------------
  164.  *
  165.  * ImgBmapCreate --
  166.  *
  167.  *    This procedure is called by the Tk image code to create "test"
  168.  *    images.
  169.  *
  170.  * Results:
  171.  *    A standard Tcl result.
  172.  *
  173.  * Side effects:
  174.  *    The data structure for a new image is allocated.
  175.  *
  176.  *----------------------------------------------------------------------
  177.  */
  178.  
  179.     /* ARGSUSED */
  180. static int
  181. ImgBmapCreate(interp, name, argc, argv, typePtr, master, clientDataPtr)
  182.     Tcl_Interp *interp;        /* Interpreter for application containing
  183.                  * image. */
  184.     char *name;            /* Name to use for image. */
  185.     int argc;            /* Number of arguments. */
  186.     char **argv;        /* Argument strings for options (doesn't
  187.                  * include image name or type). */
  188.     Tk_ImageType *typePtr;    /* Pointer to our type record (not used). */
  189.     Tk_ImageMaster master;    /* Token for image, to be used by us in
  190.                  * later callbacks. */
  191.     ClientData *clientDataPtr;    /* Store manager's token for image here;
  192.                  * it will be returned in later callbacks. */
  193. {
  194.     BitmapMaster *masterPtr;
  195.  
  196.     masterPtr = (BitmapMaster *) ckalloc(sizeof(BitmapMaster));
  197.     masterPtr->tkMaster = master;
  198.     masterPtr->interp = interp;
  199.     masterPtr->imageCmd = Tcl_CreateCommand(interp, name, ImgBmapCmd,
  200.         (ClientData) masterPtr, ImgBmapCmdDeletedProc);
  201.     masterPtr->width = masterPtr->height = 0;
  202.     masterPtr->data = NULL;
  203.     masterPtr->maskData = NULL;
  204.     masterPtr->fgUid = NULL;
  205.     masterPtr->bgUid = NULL;
  206.     masterPtr->fileString = NULL;
  207.     masterPtr->dataString = NULL;
  208.     masterPtr->maskFileString = NULL;
  209.     masterPtr->maskDataString = NULL;
  210.     masterPtr->instancePtr = NULL;
  211.     if (ImgBmapConfigureMaster(masterPtr, argc, argv, 0) != TCL_OK) {
  212.     ImgBmapDelete((ClientData) masterPtr);
  213.     return TCL_ERROR;
  214.     }
  215.     *clientDataPtr = (ClientData) masterPtr;
  216.     return TCL_OK;
  217. }
  218.  
  219. /*
  220.  *----------------------------------------------------------------------
  221.  *
  222.  * ImgBmapConfigureMaster --
  223.  *
  224.  *    This procedure is called when a bitmap image is created or
  225.  *    reconfigured.  It process configuration options and resets
  226.  *    any instances of the image.
  227.  *
  228.  * Results:
  229.  *    A standard Tcl return value.  If TCL_ERROR is returned then
  230.  *    an error message is left in masterPtr->interp->result.
  231.  *
  232.  * Side effects:
  233.  *    Existing instances of the image will be redisplayed to match
  234.  *    the new configuration options.
  235.  *
  236.  *----------------------------------------------------------------------
  237.  */
  238.  
  239. static int
  240. ImgBmapConfigureMaster(masterPtr, argc, argv, flags)
  241.     BitmapMaster *masterPtr;    /* Pointer to data structure describing
  242.                  * overall bitmap image to (reconfigure). */
  243.     int argc;            /* Number of entries in argv. */
  244.     char **argv;        /* Pairs of configuration options for image. */
  245.     int flags;            /* Flags to pass to Tk_ConfigureWidget,
  246.                  * such as TK_CONFIG_ARGV_ONLY. */
  247. {
  248.     BitmapInstance *instancePtr;
  249.     int maskWidth, maskHeight, dummy1, dummy2;
  250.  
  251.     if (Tk_ConfigureWidget(masterPtr->interp, Tk_MainWindow(masterPtr->interp),
  252.         configSpecs, argc, argv, (char *) masterPtr, flags)
  253.         != TCL_OK) {
  254.     return TCL_ERROR;
  255.     }
  256.  
  257.     /*
  258.      * Parse the bitmap and/or mask to create binary data.  Make sure that
  259.      * the bitmap and mask have the same dimensions.
  260.      */
  261.  
  262.     if (masterPtr->data != NULL) {
  263.     ckfree(masterPtr->data);
  264.     masterPtr->data = NULL;
  265.     }
  266.     if ((masterPtr->fileString != NULL) || (masterPtr->dataString != NULL)) {
  267.     masterPtr->data = TkGetBitmapData(masterPtr->interp,
  268.         masterPtr->dataString, masterPtr->fileString,
  269.         &masterPtr->width, &masterPtr->height, &dummy1, &dummy2);
  270.     if (masterPtr->data == NULL) {
  271.         return TCL_ERROR;
  272.     }
  273.     }
  274.     if (masterPtr->maskData != NULL) {
  275.     ckfree(masterPtr->maskData);
  276.     masterPtr->maskData = NULL;
  277.     }
  278.     if ((masterPtr->maskFileString != NULL)
  279.         || (masterPtr->maskDataString != NULL)) {
  280.     if (masterPtr->data == NULL) {
  281.         masterPtr->interp->result = "can't have mask without bitmap";
  282.         return TCL_ERROR;
  283.     }
  284.     masterPtr->maskData = TkGetBitmapData(masterPtr->interp,
  285.         masterPtr->maskDataString, masterPtr->maskFileString,
  286.         &maskWidth, &maskHeight, &dummy1, &dummy2);
  287.     if (masterPtr->maskData == NULL) {
  288.         return TCL_ERROR;
  289.     }
  290.     if ((maskWidth != masterPtr->width)
  291.         || (maskHeight != masterPtr->height)) {
  292.         ckfree(masterPtr->maskData);
  293.         masterPtr->maskData = NULL;
  294.         masterPtr->interp->result = "bitmap and mask have different sizes";
  295.         return TCL_ERROR;
  296.     }
  297.     }
  298.  
  299.     /*
  300.      * Cycle through all of the instances of this image, regenerating
  301.      * the information for each instance.  Then force the image to be
  302.      * redisplayed everywhere that it is used.
  303.      */
  304.  
  305.     for (instancePtr = masterPtr->instancePtr; instancePtr != NULL;
  306.         instancePtr = instancePtr->nextPtr) {
  307.     ImgBmapConfigureInstance(instancePtr);
  308.     }
  309.     Tk_ImageChanged(masterPtr->tkMaster, 0, 0, masterPtr->width,
  310.         masterPtr->height, masterPtr->width, masterPtr->height);
  311.     return TCL_OK;
  312. }
  313.  
  314. /*
  315.  *----------------------------------------------------------------------
  316.  *
  317.  * ImgBmapConfigureInstance --
  318.  *
  319.  *    This procedure is called to create displaying information for
  320.  *    a bitmap image instance based on the configuration information
  321.  *    in the master.  It is invoked both when new instances are
  322.  *    created and when the master is reconfigured.
  323.  *
  324.  * Results:
  325.  *    None.
  326.  *
  327.  * Side effects:
  328.  *    Generates errors via Tcl_BackgroundError if there are problems
  329.  *    in setting up the instance.
  330.  *
  331.  *----------------------------------------------------------------------
  332.  */
  333.  
  334. static void
  335. ImgBmapConfigureInstance(instancePtr)
  336.     BitmapInstance *instancePtr;    /* Instance to reconfigure. */
  337. {
  338.     BitmapMaster *masterPtr = instancePtr->masterPtr;
  339.     XColor *colorPtr;
  340.     XGCValues gcValues;
  341.     GC gc;
  342.     unsigned int mask;
  343.  
  344.     /*
  345.      * For each of the options in masterPtr, translate the string
  346.      * form into an internal form appropriate for instancePtr.
  347.      */
  348.  
  349.     if (*masterPtr->bgUid != 0) {
  350.     colorPtr = Tk_GetColor(masterPtr->interp, instancePtr->tkwin,
  351.         masterPtr->bgUid);
  352.     if (colorPtr == NULL) {
  353.         goto error;
  354.     }
  355.     } else {
  356.     colorPtr = NULL;
  357.     }
  358.     if (instancePtr->bg != NULL) {
  359.     Tk_FreeColor(instancePtr->bg);
  360.     }
  361.     instancePtr->bg = colorPtr;
  362.  
  363.     colorPtr = Tk_GetColor(masterPtr->interp, instancePtr->tkwin,
  364.         masterPtr->fgUid);
  365.     if (colorPtr == NULL) {
  366.     goto error;
  367.     }
  368.     if (instancePtr->fg != NULL) {
  369.     Tk_FreeColor(instancePtr->fg);
  370.     }
  371.     instancePtr->fg = colorPtr;
  372.  
  373.     if (instancePtr->bitmap != None) {
  374.     Tk_FreePixmap(Tk_Display(instancePtr->tkwin), instancePtr->bitmap);
  375.     instancePtr->bitmap = None;
  376.     }
  377.     if (masterPtr->data != NULL) {
  378.     instancePtr->bitmap = XCreateBitmapFromData(
  379.         Tk_Display(instancePtr->tkwin),
  380.         RootWindowOfScreen(Tk_Screen(instancePtr->tkwin)),
  381.         masterPtr->data, (unsigned) masterPtr->width,
  382.         (unsigned) masterPtr->height);
  383.     }
  384.  
  385.     if (instancePtr->mask != None) {
  386.     Tk_FreePixmap(Tk_Display(instancePtr->tkwin), instancePtr->mask);
  387.     instancePtr->mask = None;
  388.     }
  389.     if (masterPtr->maskData != NULL) {
  390.     instancePtr->mask = XCreateBitmapFromData(
  391.         Tk_Display(instancePtr->tkwin),
  392.         RootWindowOfScreen(Tk_Screen(instancePtr->tkwin)),
  393.         masterPtr->maskData, (unsigned) masterPtr->width,
  394.         (unsigned) masterPtr->height);
  395.     }
  396.  
  397.     if (masterPtr->data != NULL) {
  398.     gcValues.foreground = instancePtr->fg->pixel;
  399.     gcValues.graphics_exposures = False;
  400.     mask = GCForeground|GCGraphicsExposures;
  401.     if (instancePtr->bg != NULL) {
  402.         gcValues.background = instancePtr->bg->pixel;
  403.         mask |= GCBackground;
  404.         if (instancePtr->mask != None) {
  405.         gcValues.clip_mask = instancePtr->mask;
  406.         mask |= GCClipMask;
  407.         }
  408.     } else {
  409.         gcValues.clip_mask = instancePtr->bitmap;
  410.         mask |= GCClipMask;
  411.     }
  412.     gc = Tk_GetGC(instancePtr->tkwin, mask, &gcValues);
  413.     } else {
  414.     gc = None;
  415.     }
  416.     if (instancePtr->gc != None) {
  417.     Tk_FreeGC(Tk_Display(instancePtr->tkwin), instancePtr->gc);
  418.     }
  419.     instancePtr->gc = gc;
  420.     return;
  421.  
  422.     error:
  423.     /*
  424.      * An error occurred: clear the graphics context in the instance to
  425.      * make it clear that this instance cannot be displayed.  Then report
  426.      * the error.
  427.      */
  428.  
  429.     if (instancePtr->gc != None) {
  430.     Tk_FreeGC(Tk_Display(instancePtr->tkwin), instancePtr->gc);
  431.     }
  432.     instancePtr->gc = None;
  433.     Tcl_AddErrorInfo(masterPtr->interp, "\n    (while configuring image \"");
  434.     Tcl_AddErrorInfo(masterPtr->interp, Tk_NameOfImage(masterPtr->tkMaster));
  435.     Tcl_AddErrorInfo(masterPtr->interp, "\")");
  436.     Tcl_BackgroundError(masterPtr->interp);
  437. }
  438.  
  439. /*
  440.  *----------------------------------------------------------------------
  441.  *
  442.  * TkGetBitmapData --
  443.  *
  444.  *    Given a file name or ASCII string, this procedure parses the
  445.  *    file or string contents to produce binary data for a bitmap.
  446.  *
  447.  * Results:
  448.  *    If the bitmap description was parsed successfully then the
  449.  *    return value is a malloc-ed array containing the bitmap data.
  450.  *    The dimensions of the data are stored in *widthPtr and
  451.  *    *heightPtr.  *hotXPtr and *hotYPtr are set to the bitmap
  452.  *    hotspot if one is defined, otherwise they are set to -1, -1.
  453.  *    If an error occurred, NULL is returned and an error message is
  454.  *    left in interp->result.
  455.  *
  456.  * Side effects:
  457.  *    A bitmap is created.
  458.  *
  459.  *----------------------------------------------------------------------
  460.  */
  461.  
  462. char *
  463. TkGetBitmapData(interp, string, fileName, widthPtr, heightPtr,
  464.     hotXPtr, hotYPtr)
  465.     Tcl_Interp *interp;            /* For reporting errors. */
  466.     char *string;            /* String describing bitmap.  May
  467.                      * be NULL. */
  468.     char *fileName;            /* Name of file containing bitmap
  469.                      * description.  Used only if string
  470.                      * is NULL.  Must not be NULL if
  471.                      * string is NULL. */
  472.     int *widthPtr, *heightPtr;        /* Dimensions of bitmap get returned
  473.                      * here. */
  474.     int *hotXPtr, *hotYPtr;        /* Position of hot spot or -1,-1. */
  475. {
  476.     int width, height, numBytes, hotX, hotY;
  477.     char *p, *end, *expandedFileName;
  478.     ParseInfo pi;
  479.     char *data = NULL;
  480.     Tcl_DString buffer;
  481.  
  482.     pi.string = string;
  483.     if (string == NULL) {
  484.         if (Tcl_IsSafe(interp)) {
  485.             Tcl_AppendResult(interp, "can't get bitmap data from a file in a",
  486.                     " safe interpreter", (char *) NULL);
  487.             return NULL;
  488.         }
  489.     expandedFileName = Tcl_TranslateFileName(interp, fileName, &buffer);
  490.     if (expandedFileName == NULL) {
  491.         return NULL;
  492.     }
  493.     pi.chan = Tcl_OpenFileChannel(interp, expandedFileName, "r", 0);
  494.     Tcl_DStringFree(&buffer);
  495.     if (pi.chan == NULL) {
  496.         Tcl_ResetResult(interp);
  497.         Tcl_AppendResult(interp, "couldn't read bitmap file \"",
  498.             fileName, "\": ", Tcl_PosixError(interp), (char *) NULL);
  499.         return NULL;
  500.     }
  501.     } else {
  502.     pi.chan = NULL;
  503.     }
  504.  
  505.     /*
  506.      * Parse the lines that define the dimensions of the bitmap,
  507.      * plus the first line that defines the bitmap data (it declares
  508.      * the name of a data variable but doesn't include any actual
  509.      * data).  These lines look something like the following:
  510.      *
  511.      *        #define foo_width 16
  512.      *        #define foo_height 16
  513.      *        #define foo_x_hot 3
  514.      *        #define foo_y_hot 3
  515.      *        static char foo_bits[] = {
  516.      *
  517.      * The x_hot and y_hot lines may or may not be present.  It's
  518.      * important to check for "char" in the last line, in order to
  519.      * reject old X10-style bitmaps that used shorts.
  520.      */
  521.  
  522.     width = 0;
  523.     height = 0;
  524.     hotX = -1;
  525.     hotY = -1;
  526.     while (1) {
  527.     if (NextBitmapWord(&pi) != TCL_OK) {
  528.         goto error;
  529.     }
  530.     if ((pi.wordLength >= 6) && (pi.word[pi.wordLength-6] == '_')
  531.         && (strcmp(pi.word+pi.wordLength-6, "_width") == 0)) {
  532.         if (NextBitmapWord(&pi) != TCL_OK) {
  533.         goto error;
  534.         }
  535.         width = strtol(pi.word, &end, 0);
  536.         if ((end == pi.word) || (*end != 0)) {
  537.         goto error;
  538.         }
  539.     } else if ((pi.wordLength >= 7) && (pi.word[pi.wordLength-7] == '_')
  540.         && (strcmp(pi.word+pi.wordLength-7, "_height") == 0)) {
  541.         if (NextBitmapWord(&pi) != TCL_OK) {
  542.         goto error;
  543.         }
  544.         height = strtol(pi.word, &end, 0);
  545.         if ((end == pi.word) || (*end != 0)) {
  546.         goto error;
  547.         }
  548.     } else if ((pi.wordLength >= 6) && (pi.word[pi.wordLength-6] == '_')
  549.         && (strcmp(pi.word+pi.wordLength-6, "_x_hot") == 0)) {
  550.         if (NextBitmapWord(&pi) != TCL_OK) {
  551.         goto error;
  552.         }
  553.         hotX = strtol(pi.word, &end, 0);
  554.         if ((end == pi.word) || (*end != 0)) {
  555.         goto error;
  556.         }
  557.     } else if ((pi.wordLength >= 6) && (pi.word[pi.wordLength-6] == '_')
  558.         && (strcmp(pi.word+pi.wordLength-6, "_y_hot") == 0)) {
  559.         if (NextBitmapWord(&pi) != TCL_OK) {
  560.         goto error;
  561.         }
  562.         hotY = strtol(pi.word, &end, 0);
  563.         if ((end == pi.word) || (*end != 0)) {
  564.         goto error;
  565.         }
  566.     } else if ((pi.word[0] == 'c') && (strcmp(pi.word, "char") == 0)) {
  567.         while (1) {
  568.         if (NextBitmapWord(&pi) != TCL_OK) {
  569.             goto error;
  570.         }
  571.         if ((pi.word[0] == '{') && (pi.word[1] == 0)) {
  572.             goto getData;
  573.         }
  574.         }
  575.     } else if ((pi.word[0] == '{') && (pi.word[1] == 0)) {
  576.         Tcl_AppendResult(interp, "format error in bitmap data; ",
  577.             "looks like it's an obsolete X10 bitmap file",
  578.             (char *) NULL);
  579.         goto errorCleanup;
  580.     }
  581.     }
  582.  
  583.     /*
  584.      * Now we've read everything but the data.  Allocate an array
  585.      * and read in the data.
  586.      */
  587.  
  588.     getData:
  589.     if ((width <= 0) || (height <= 0)) {
  590.     goto error;
  591.     }
  592.     numBytes = ((width+7)/8) * height;
  593.     data = (char *) ckalloc((unsigned) numBytes);
  594.     for (p = data; numBytes > 0; p++, numBytes--) {
  595.     if (NextBitmapWord(&pi) != TCL_OK) {
  596.         goto error;
  597.     }
  598.     *p = (char) strtol(pi.word, &end, 0);
  599.     if (end == pi.word) {
  600.         goto error;
  601.     }
  602.     }
  603.  
  604.     /*
  605.      * All done.  Clean up and return.
  606.      */
  607.  
  608.     if (pi.chan != NULL) {
  609.     Tcl_Close(NULL, pi.chan);
  610.     }
  611.     *widthPtr = width;
  612.     *heightPtr = height;
  613.     *hotXPtr = hotX;
  614.     *hotYPtr = hotY;
  615.     return data;
  616.  
  617.     error:
  618.     interp->result = "format error in bitmap data";
  619.     errorCleanup:
  620.     if (data != NULL) {
  621.     ckfree(data);
  622.     }
  623.     if (pi.chan != NULL) {
  624.     Tcl_Close(NULL, pi.chan);
  625.     }
  626.     return NULL;
  627. }
  628.  
  629. /*
  630.  *----------------------------------------------------------------------
  631.  *
  632.  * NextBitmapWord --
  633.  *
  634.  *    This procedure retrieves the next word of information (stuff
  635.  *    between commas or white space) from a bitmap description.
  636.  *
  637.  * Results:
  638.  *    Returns TCL_OK if all went well.  In this case the next word,
  639.  *    and its length, will be availble in *parseInfoPtr.  If the end
  640.  *    of the bitmap description was reached then TCL_ERROR is returned.
  641.  *
  642.  * Side effects:
  643.  *    None.
  644.  *
  645.  *----------------------------------------------------------------------
  646.  */
  647.  
  648. static int
  649. NextBitmapWord(parseInfoPtr)
  650.     ParseInfo *parseInfoPtr;        /* Describes what we're reading
  651.                      * and where we are in it. */
  652. {
  653.     char *src, *dst;
  654.     int c;
  655.  
  656.     parseInfoPtr->wordLength = 0;
  657.     dst = parseInfoPtr->word;
  658.     if (parseInfoPtr->string != NULL) {
  659.     for (src = parseInfoPtr->string; isspace(UCHAR(*src)) || (*src == ',');
  660.         src++) {
  661.         if (*src == 0) {
  662.         return TCL_ERROR;
  663.         }
  664.     }
  665.     for ( ; !isspace(UCHAR(*src)) && (*src != ',') && (*src != 0); src++) {
  666.         *dst = *src;
  667.         dst++;
  668.         parseInfoPtr->wordLength++;
  669.         if (parseInfoPtr->wordLength > MAX_WORD_LENGTH) {
  670.         return TCL_ERROR;
  671.         }
  672.     }
  673.     parseInfoPtr->string = src;
  674.     } else {
  675.     for (c = GetByte(parseInfoPtr->chan); isspace(UCHAR(c)) || (c == ',');
  676.         c = GetByte(parseInfoPtr->chan)) {
  677.         if (c == EOF) {
  678.         return TCL_ERROR;
  679.         }
  680.     }
  681.     for ( ; !isspace(UCHAR(c)) && (c != ',') && (c != EOF);
  682.         c = GetByte(parseInfoPtr->chan)) {
  683.         *dst = c;
  684.         dst++;
  685.         parseInfoPtr->wordLength++;
  686.         if (parseInfoPtr->wordLength > MAX_WORD_LENGTH) {
  687.         return TCL_ERROR;
  688.         }
  689.     }
  690.     }
  691.     if (parseInfoPtr->wordLength == 0) {
  692.     return TCL_ERROR;
  693.     }
  694.     parseInfoPtr->word[parseInfoPtr->wordLength] = 0;
  695.     return TCL_OK;
  696. }
  697.  
  698. /*
  699.  *--------------------------------------------------------------
  700.  *
  701.  * ImgBmapCmd --
  702.  *
  703.  *    This procedure is invoked to process the Tcl command
  704.  *    that corresponds to an image managed by this module.
  705.  *    See the user documentation for details on what it does.
  706.  *
  707.  * Results:
  708.  *    A standard Tcl result.
  709.  *
  710.  * Side effects:
  711.  *    See the user documentation.
  712.  *
  713.  *--------------------------------------------------------------
  714.  */
  715.  
  716. static int
  717. ImgBmapCmd(clientData, interp, argc, argv)
  718.     ClientData clientData;    /* Information about the image master. */
  719.     Tcl_Interp *interp;        /* Current interpreter. */
  720.     int argc;            /* Number of arguments. */
  721.     char **argv;        /* Argument strings. */
  722. {
  723.     BitmapMaster *masterPtr = (BitmapMaster *) clientData;
  724.     int c, code;
  725.     size_t length;
  726.  
  727.     if (argc < 2) {
  728.     sprintf(interp->result,
  729.         "wrong # args: should be \"%.50s option ?arg arg ...?\"",
  730.         argv[0]);
  731.     return TCL_ERROR;
  732.     }
  733.     c = argv[1][0];
  734.     length = strlen(argv[1]);
  735.     if ((c == 'c') && (strncmp(argv[1], "cget", length) == 0)
  736.         && (length >= 2)) {
  737.     if (argc != 3) {
  738.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  739.             argv[0], " cget option\"",
  740.             (char *) NULL);
  741.         return TCL_ERROR;
  742.     }
  743.     return Tk_ConfigureValue(interp, Tk_MainWindow(interp), configSpecs,
  744.         (char *) masterPtr, argv[2], 0);
  745.     } else if ((c == 'c') && (strncmp(argv[1], "configure", length) == 0)
  746.         && (length >= 2)) {
  747.     if (argc == 2) {
  748.         code = Tk_ConfigureInfo(interp, Tk_MainWindow(interp),
  749.             configSpecs, (char *) masterPtr, (char *) NULL, 0);
  750.     } else if (argc == 3) {
  751.         code = Tk_ConfigureInfo(interp, Tk_MainWindow(interp),
  752.             configSpecs, (char *) masterPtr, argv[2], 0);
  753.     } else {
  754.         code = ImgBmapConfigureMaster(masterPtr, argc-2, argv+2,
  755.             TK_CONFIG_ARGV_ONLY);
  756.     }
  757.     return code;
  758.     } else {
  759.     Tcl_AppendResult(interp, "bad option \"", argv[1],
  760.         "\": must be cget or configure", (char *) NULL);
  761.     return TCL_ERROR;
  762.     }
  763. }
  764.  
  765. /*
  766.  *----------------------------------------------------------------------
  767.  *
  768.  * ImgBmapGet --
  769.  *
  770.  *    This procedure is called for each use of a bitmap image in a
  771.  *    widget.
  772.  *
  773.  * Results:
  774.  *    The return value is a token for the instance, which is passed
  775.  *    back to us in calls to ImgBmapDisplay and ImgBmapFree.
  776.  *
  777.  * Side effects:
  778.  *    A data structure is set up for the instance (or, an existing
  779.  *    instance is re-used for the new one).
  780.  *
  781.  *----------------------------------------------------------------------
  782.  */
  783.  
  784. static ClientData
  785. ImgBmapGet(tkwin, masterData)
  786.     Tk_Window tkwin;        /* Window in which the instance will be
  787.                  * used. */
  788.     ClientData masterData;    /* Pointer to our master structure for the
  789.                  * image. */
  790. {
  791.     BitmapMaster *masterPtr = (BitmapMaster *) masterData;
  792.     BitmapInstance *instancePtr;
  793.  
  794.     /*
  795.      * See if there is already an instance for this window.  If so
  796.      * then just re-use it.
  797.      */
  798.  
  799.     for (instancePtr = masterPtr->instancePtr; instancePtr != NULL;
  800.         instancePtr = instancePtr->nextPtr) {
  801.     if (instancePtr->tkwin == tkwin) {
  802.         instancePtr->refCount++;
  803.         return (ClientData) instancePtr;
  804.     }
  805.     }
  806.  
  807.     /*
  808.      * The image isn't already in use in this window.  Make a new
  809.      * instance of the image.
  810.      */
  811.  
  812.     instancePtr = (BitmapInstance *) ckalloc(sizeof(BitmapInstance));
  813.     instancePtr->refCount = 1;
  814.     instancePtr->masterPtr = masterPtr;
  815.     instancePtr->tkwin = tkwin;
  816.     instancePtr->fg = NULL;
  817.     instancePtr->bg = NULL;
  818.     instancePtr->bitmap = None;
  819.     instancePtr->mask = None;
  820.     instancePtr->gc = None;
  821.     instancePtr->nextPtr = masterPtr->instancePtr;
  822.     masterPtr->instancePtr = instancePtr;
  823.     ImgBmapConfigureInstance(instancePtr);
  824.  
  825.     /*
  826.      * If this is the first instance, must set the size of the image.
  827.      */
  828.  
  829.     if (instancePtr->nextPtr == NULL) {
  830.     Tk_ImageChanged(masterPtr->tkMaster, 0, 0, 0, 0, masterPtr->width,
  831.         masterPtr->height);
  832.     }
  833.  
  834.     return (ClientData) instancePtr;
  835. }
  836.  
  837. /*
  838.  *----------------------------------------------------------------------
  839.  *
  840.  * ImgBmapDisplay --
  841.  *
  842.  *    This procedure is invoked to draw a bitmap image.
  843.  *
  844.  * Results:
  845.  *    None.
  846.  *
  847.  * Side effects:
  848.  *    A portion of the image gets rendered in a pixmap or window.
  849.  *
  850.  *----------------------------------------------------------------------
  851.  */
  852.  
  853. static void
  854. ImgBmapDisplay(clientData, display, drawable, imageX, imageY, width,
  855.     height, drawableX, drawableY)
  856.     ClientData clientData;    /* Pointer to BitmapInstance structure for
  857.                  * for instance to be displayed. */
  858.     Display *display;        /* Display on which to draw image. */
  859.     Drawable drawable;        /* Pixmap or window in which to draw image. */
  860.     int imageX, imageY;        /* Upper-left corner of region within image
  861.                  * to draw. */
  862.     int width, height;        /* Dimensions of region within image to draw. */
  863.     int drawableX, drawableY;    /* Coordinates within drawable that
  864.                  * correspond to imageX and imageY. */
  865. {
  866.     BitmapInstance *instancePtr = (BitmapInstance *) clientData;
  867.     int masking;
  868.  
  869.     /*
  870.      * If there's no graphics context, it means that an error occurred
  871.      * while creating the image instance so it can't be displayed.
  872.      */
  873.  
  874.     if (instancePtr->gc == None) {
  875.     return;
  876.     }
  877.  
  878.     /*
  879.      * If masking is in effect, must modify the mask origin within
  880.      * the graphics context to line up with the image's origin.
  881.      * Then draw the image and reset the clip origin, if there's
  882.      * a mask.
  883.      */
  884.  
  885.     masking = (instancePtr->mask != None) || (instancePtr->bg == NULL);
  886.     if (masking) {
  887.     XSetClipOrigin(display, instancePtr->gc, drawableX - imageX,
  888.         drawableY - imageY);
  889.     }
  890.     XCopyPlane(display, instancePtr->bitmap, drawable, instancePtr->gc,
  891.         imageX, imageY, (unsigned) width, (unsigned) height,
  892.         drawableX, drawableY, 1);
  893.     if (masking) {
  894.     XSetClipOrigin(display, instancePtr->gc, 0, 0);
  895.     }
  896. }
  897.  
  898. /*
  899.  *----------------------------------------------------------------------
  900.  *
  901.  * ImgBmapFree --
  902.  *
  903.  *    This procedure is called when a widget ceases to use a
  904.  *    particular instance of an image.
  905.  *
  906.  * Results:
  907.  *    None.
  908.  *
  909.  * Side effects:
  910.  *    Internal data structures get cleaned up.
  911.  *
  912.  *----------------------------------------------------------------------
  913.  */
  914.  
  915. static void
  916. ImgBmapFree(clientData, display)
  917.     ClientData clientData;    /* Pointer to BitmapInstance structure for
  918.                  * for instance to be displayed. */
  919.     Display *display;        /* Display containing window that used image. */
  920. {
  921.     BitmapInstance *instancePtr = (BitmapInstance *) clientData;
  922.     BitmapInstance *prevPtr;
  923.  
  924.     instancePtr->refCount--;
  925.     if (instancePtr->refCount > 0) {
  926.     return;
  927.     }
  928.  
  929.     /*
  930.      * There are no more uses of the image within this widget.  Free
  931.      * the instance structure.
  932.      */
  933.  
  934.     if (instancePtr->fg != NULL) {
  935.     Tk_FreeColor(instancePtr->fg);
  936.     }
  937.     if (instancePtr->bg != NULL) {
  938.     Tk_FreeColor(instancePtr->bg);
  939.     }
  940.     if (instancePtr->bitmap != None) {
  941.     Tk_FreePixmap(display, instancePtr->bitmap);
  942.     }
  943.     if (instancePtr->mask != None) {
  944.     Tk_FreePixmap(display, instancePtr->mask);
  945.     }
  946.     if (instancePtr->gc != None) {
  947.     Tk_FreeGC(display, instancePtr->gc);
  948.     }
  949.     if (instancePtr->masterPtr->instancePtr == instancePtr) {
  950.     instancePtr->masterPtr->instancePtr = instancePtr->nextPtr;
  951.     } else {
  952.     for (prevPtr = instancePtr->masterPtr->instancePtr;
  953.         prevPtr->nextPtr != instancePtr; prevPtr = prevPtr->nextPtr) {
  954.         /* Empty loop body */
  955.     }
  956.     prevPtr->nextPtr = instancePtr->nextPtr;
  957.     }
  958.     ckfree((char *) instancePtr);
  959. }
  960.  
  961. /*
  962.  *----------------------------------------------------------------------
  963.  *
  964.  * ImgBmapDelete --
  965.  *
  966.  *    This procedure is called by the image code to delete the
  967.  *    master structure for an image.
  968.  *
  969.  * Results:
  970.  *    None.
  971.  *
  972.  * Side effects:
  973.  *    Resources associated with the image get freed.
  974.  *
  975.  *----------------------------------------------------------------------
  976.  */
  977.  
  978. static void
  979. ImgBmapDelete(masterData)
  980.     ClientData masterData;    /* Pointer to BitmapMaster structure for
  981.                  * image.  Must not have any more instances. */
  982. {
  983.     BitmapMaster *masterPtr = (BitmapMaster *) masterData;
  984.  
  985.     if (masterPtr->instancePtr != NULL) {
  986.     panic("tried to delete bitmap image when instances still exist");
  987.     }
  988.     masterPtr->tkMaster = NULL;
  989.     if (masterPtr->imageCmd != NULL) {
  990.     Tcl_DeleteCommandFromToken(masterPtr->interp, masterPtr->imageCmd);
  991.     }
  992.     if (masterPtr->data != NULL) {
  993.     ckfree(masterPtr->data);
  994.     }
  995.     if (masterPtr->maskData != NULL) {
  996.     ckfree(masterPtr->maskData);
  997.     }
  998.     Tk_FreeOptions(configSpecs, (char *) masterPtr, (Display *) NULL, 0);
  999.     ckfree((char *) masterPtr);
  1000. }
  1001.  
  1002. /*
  1003.  *----------------------------------------------------------------------
  1004.  *
  1005.  * ImgBmapCmdDeletedProc --
  1006.  *
  1007.  *    This procedure is invoked when the image command for an image
  1008.  *    is deleted.  It deletes the image.
  1009.  *
  1010.  * Results:
  1011.  *    None.
  1012.  *
  1013.  * Side effects:
  1014.  *    The image is deleted.
  1015.  *
  1016.  *----------------------------------------------------------------------
  1017.  */
  1018.  
  1019. static void
  1020. ImgBmapCmdDeletedProc(clientData)
  1021.     ClientData clientData;    /* Pointer to BitmapMaster structure for
  1022.                  * image. */
  1023. {
  1024.     BitmapMaster *masterPtr = (BitmapMaster *) clientData;
  1025.  
  1026.     masterPtr->imageCmd = NULL;
  1027.     if (masterPtr->tkMaster != NULL) {
  1028.     Tk_DeleteImage(masterPtr->interp, Tk_NameOfImage(masterPtr->tkMaster));
  1029.     }
  1030. }
  1031.  
  1032. /*
  1033.  *----------------------------------------------------------------------
  1034.  *
  1035.  * GetByte --
  1036.  *
  1037.  *    Get the next byte from the open channel.
  1038.  *
  1039.  * Results:
  1040.  *    The next byte or EOF.
  1041.  *
  1042.  * Side effects:
  1043.  *    We read from the channel.
  1044.  *
  1045.  *----------------------------------------------------------------------
  1046.  */
  1047.  
  1048. static int
  1049. GetByte(chan)
  1050.     Tcl_Channel chan;    /* The channel we read from. */
  1051. {
  1052.     char buffer;
  1053.     int size;
  1054.  
  1055.     size = Tcl_Read(chan, &buffer, 1);
  1056.     if (size <= 0) {
  1057.     return EOF;
  1058.     } else {
  1059.     return buffer;
  1060.     }
  1061. }
  1062.